package com.tepth.busfix.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tepth.busfix.R;
import com.tepth.busfix.bean.reponse.FaultSection;
import com.tepth.busfix.bean.reponse.Trouble;
import com.tepth.busfix.bean.reponse.TroubleContent;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.List;

/**
 * Created by pc on 2017/3/16.
 */

public class RepairTypeAdapter extends BaseSectionQuickAdapter<FaultSection, BaseViewHolder> {
    private LayoutInflater mInflater;
    private OnTagClickListener mOnTagClickListener;
    private Drawable mColorSelect;
    private Drawable mColorNormal;
    private int mTextColorWhite;
    private int mTextColorBlack;
    private int mTextColorRed;

    public RepairTypeAdapter(Context context, int layoutResId,
                             int sectionHeadResId, List<FaultSection> data
            , OnTagClickListener listener) {
        super(layoutResId, sectionHeadResId, data);
        mInflater = LayoutInflater.from(context);
        mOnTagClickListener = listener;
        mColorSelect = context.getResources().getDrawable(R.drawable.bg_tag_select);
        mColorNormal = context.getResources().getDrawable(R.drawable.bg_tag_normal);
        mTextColorWhite = context.getResources().getColor(R.color.green_scan_to_barcode_p);
        mTextColorBlack = context.getResources().getColor(R.color.text_black_87);
        mTextColorRed = context.getResources().getColor(R.color.color_red);
    }

    @Override
    protected void convertHead(BaseViewHolder helper, FaultSection item) {
        helper.setText(R.id.tv_header, item.header);
    }

    @Override
    protected void convert(BaseViewHolder helper, FaultSection item) {

        TagFlowLayout tagFlowLayout = helper.getView(R.id.id_tag_flow_layout);
        final TagAdapter<Trouble> tagAdapter = new TagAdapter<Trouble>(item.t) {
            @Override
            public View getView(FlowLayout parent, int position, Trouble fault) {
                TextView tv = (TextView) mInflater.inflate(R.layout.view_tv,
                        parent, false);
                tv.setText(fault.getName());
                if (fault.isChecked()) {
                    tv.setTextColor(mTextColorWhite);
                } else {
                    tv.setTextColor(mTextColorRed);
                }
                return tv;
            }
        };
        tagFlowLayout.setAdapter(tagAdapter);
        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                mOnTagClickListener.onTagClick(tagAdapter.getItem(position));
                return false;
            }
        });
    }

    public void updateTroubleState(long id, TroubleContent troubleContent) {
        List<FaultSection> list = getData();
        for (FaultSection faultSection : list) {
            List<Trouble> troubleList = faultSection.t;
            if (troubleList != null) {
                for (Trouble trouble : troubleList) {
                    if (trouble.getId() == id) {
                        trouble.getListContent().remove(troubleContent);
                        if (trouble.getListContent().isEmpty()) {
                            trouble.setChecked(false);
                        } else {
                            trouble.setChecked(true);
                        }
                    }
                }
            }
        }
        notifyDataSetChanged();
    }

    public interface OnTagClickListener {
        void onTagClick(Trouble trouble);
    }
}
